package pers.vinsonws.jianghu.clientwin.test;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import pers.vinsonws.jianghu.util.ChatRoom;
import pers.vinsonws.jianghu.util.MsgRoom;
import pers.vinsonws.jianghu.util.User;

import java.util.Date;
import java.util.Random;

/**
 * @auther vinsonws
 * @date 2018/10/30 17:31
 */
public class ObjectClientHandler extends SimpleChannelInboundHandler<Object> {
    @Override
    protected void channelRead0(ChannelHandlerContext arg0, Object obj)
            throws Exception {
        //MsgRoom myObject=(MsgRoom)obj;
        System.out.println("client receive" + obj);
    }
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //channel连接成功时发送
        Random random=new Random();
        StringBuilder sb=new StringBuilder();
        User user =new User();
        user.setId(1);
        ChatRoom chatRoom = new ChatRoom();
        chatRoom.setId(2);
        for(int i=0;i<10;i++){
            MsgRoom myObject=new MsgRoom();
            sb.append("a");
            myObject.setUserSendId(user.getId());
            myObject.setChatRoomId(chatRoom.getId());
            myObject.setDateLong(System.currentTimeMillis());
            myObject.setMessage(sb.toString());
            ctx.write(myObject);
        }
        ctx.flush();
        System.out.println("send seccess");
    }
}
